package cn.bluethink.eguan.core.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Id;
import javax.persistence.Table;

import cn.bluethink.eguan.model.core.EgFile;
import cn.bluethink.eguan.model.core.EgOType;
import cn.bluethink.eguan.model.core.EgObject;
import cn.bluethink.eguan.model.core.EgUser;

/**
 * 系统文件实体模型
 * 
 * @author 苏州中科蓝迪公司所有(c)2016-2021
 * @version 0.3.0
 * @brief 驿馆+产品
 * @note 修订历史： 1.wanglei于2018/12/20设计并构建初始版本v0.3.0
 */
@Table(name = "eg_file")
public class EgFileEntity implements Serializable {

    private static final long serialVersionUID = -1734994885281695669L;

    /*
     * 主键ID
     */
    @Id
    private Long id;

    /*
     * 文件唯一标识（上传后将获取到的文件标识存放到本地）
     */
    private String fileId;

    /*
     * 文件名（包括后缀名）
     */
    private String name;

    /*
     * 文件所在位置对象Id
     */
    private Long posOid;

    /*
     * 文件所在位置对象类型Id
     */
    private Integer posOtype;

    /*
     * 文件上传者
     */
    private Long uploaderId;

    /*
     * 文件的上传时间
     */
    private Date ctime;

    public EgFileEntity() {

    }

    /**
     * 创建实体
     * @param file
     */
    public EgFileEntity(EgFile file) {
        this.id = file.getId();
        this.fileId = file.getFile() == null ? null : file.getFile();
        this.name = file.getName();
        this.posOid = file.getPos() == null ? null : file.getPos().getId();
        this.posOtype = file.getPos() == null ? null : file.getPos().getType().getOtid();
        this.uploaderId = file.getUploader() == null ? null : file.getUploader().getId();
        this.ctime = new Date();
    }

    /**
     * 转业务模型
     * @return
     */
    public EgFile createEgFile() {
        EgFile file = new EgFile(id, name, fileId);
        if (posOid != null && posOtype != null) {
            file.setPos(new EgObject(posOid, null, new EgOType(posOtype, null)));
        } else {
            file.setPos(null);
        }
        if (uploaderId != null && uploaderId > 0) {
            file.setUploader(new EgUser(uploaderId, null, null));
        } else {
            file.setUploader(null);
        }
        file.setCtime(ctime);
        return file;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFileId() {
        return fileId;
    }

    public void setFileId(String fileId) {
        this.fileId = fileId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getPosOid() {
        return posOid;
    }

    public void setPosOid(Long posOid) {
        this.posOid = posOid;
    }

    public Integer getPosOtype() {
        return posOtype;
    }

    public void setPosOtype(Integer posOtype) {
        this.posOtype = posOtype;
    }

    public Long getUploaderId() {
        return uploaderId;
    }

    public void setUploaderId(Long uploaderId) {
        this.uploaderId = uploaderId;
    }

    public Date getCtime() {
        return ctime;
    }

    public void setCtime(Date ctime) {
        this.ctime = ctime;
    }

}